//
//  SLBadgeButton.m
//  OSPMobile
//
//  Created by 王胜利 on 2017/12/20.
//  Copyright © 2017年 Pansoft. All rights reserved.
//  Page标题栏带Badge按钮
//

#import "SLBadgeButton.h"
//#import "Constant.h"
#import "SLPageManager.h"

@interface SLBadgeButton()

/// 未读消息数
@property (strong, nonatomic) UIButton *badgeBtn;
/// 小红点
@property (strong, nonatomic) UIView  *redPointView;

@end
@implementation SLBadgeButton

- (instancetype)init{
    self = [super init];
    if (self) {
        self.isShowRedPoint = NO;
    }
    return self;
}

- (void)setBadge:(NSString *)badge{
    _badge = badge;

    if (!badge || badge.length == 0) {
        self.badgeBtn.hidden = YES;
        return;
    }

    self.badgeBtn.hidden = [badge isEqualToString:@"0"];

    // 加载小红点提醒
    if (badge && badge.length >= 1 && ![badge isEqualToString:@"0"]) {
        [self.badgeBtn setTitle:badge forState:UIControlStateNormal];
        CGSize size = [badge sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.0]}];
        CGFloat badgeW = size.width<size.height?size.height:size.width;
        [self.badgeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(badgeW + 6, size.height + 6));
        }];
        self.badgeBtn.layer.cornerRadius = size.height/2 + 3;
    }
}

- (void)setIsShowRedPoint:(BOOL)isShowRedPoint{
    _isShowRedPoint = isShowRedPoint;
    self.redPointView.hidden = !isShowRedPoint;
}


- (void)setPageEntity:(SLPageEntity *)pageEntity{
    _pageEntity = pageEntity;
    self.badge = pageEntity.badge;
    self.isShowRedPoint = pageEntity.isShowRedPoint;
    [self setTitle:pageEntity.title forState:UIControlStateNormal];
}

#pragma mark - lazyload
- (UIButton *)badgeBtn{
    if (!_badgeBtn) {
        _badgeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_badgeBtn setBackgroundImage:[self imageWithColor:UIColor.redColor] forState:UIControlStateNormal];
        _badgeBtn.userInteractionEnabled = NO;
        [_badgeBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _badgeBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _badgeBtn.layer.cornerRadius = 5;
        _badgeBtn.layer.masksToBounds = YES;
        _badgeBtn.titleLabel.font = [UIFont systemFontOfSize:10.0];
        [self addSubview:_badgeBtn];
        [_badgeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(3);
            make.right.equalTo(self).offset(-3);
            make.size.mas_equalTo(CGSizeMake(10, 10));
        }];
    }
    return _badgeBtn;
}


- (UIImage *)imageWithColor:(UIColor *)color{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


-(UIView *)redPointView{
    if(!_redPointView) {
        _redPointView = [[UIView alloc]init];
        _redPointView.backgroundColor = UIColor.redColor;
        [self addSubview:_redPointView];
        [_redPointView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(8, 8)));
            make.top.equalTo(self.mas_top).inset(4);
            make.right.equalTo(self.mas_right).inset(4);
        }];
        _redPointView.layer.cornerRadius = 4;
        _redPointView.layer.masksToBounds = YES;
    }
    return _redPointView;
}



@end
